<?php
/**
 * alltosun.com 余额类型控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id$
*/

class Action
{   
    //声明每页显示的条数
    private $per_page = 10;

    /**
    * 显示余额类型列表
    */
    public function index()
    {
        $page          = Request::getParam('page_no', 1);
        $status        = Request::getParam('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter           = array();
        $filter['status'] = $status;

        //先获取所有的父级分类的id
        $parent_type_list = _model('user_money_type')->getList(array('status'=>$status, 'parent_id'=>0));

        $parent_ids = array();
        $user_money_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {
                array_push($parent_ids, $value['id']);
                $user_money_type_list[$key]['id'] = $value['id'];
                $user_money_type_list[$key]['res_name'] = $value['res_name'];
                $user_money_type_list[$key]['type_list'] = _model('user_money_type')->getList(array('status'=>$status, 'parent_id'=>$value['id']));
            }
        }

        Response::assign('user_money_type_list', $user_money_type_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        Response::display('admin/user_money_type_list.html');

    }

    /**
    * 显示添加页面
    */
    public function add()
    {
        //获取所有的父级类型列表
        $filter = array();
        $filter['status']    = 1;
        $filter['parent_id'] = 0;

        $parent_user_money_type_list = _model('user_money_type')->getList($filter);

        Response::assign('parent_user_money_type_list', $parent_user_money_type_list);
        Response::display('admin/add_user_money_type.html');
    }

    /**
    * 添加
    */
    public function create()
    {
        $type_info = Request::getParam('user_money_type', array());

        //检测余额类型是否存在
        $user_money_type_info = _model('user_money_type')->read(array('status'=>1, 'res_name'=>$type_info['res_name']));
        if ($user_money_type_info) {
            return '余额类型已经存在了';
        }
        //创建数据
        $info = array();
        $info['res_name']  = $type_info['res_name'];
        $info['parent_id'] = $type_info['parent_id'];

        _model('user_money_type')->create($info);

        Response::redirect('user_money/admin/user_money_type.html');

    }

    /**
    * 编辑
    */
    public function edit()
    {
        $id = Request::getParam('id', 1);
        $user_money_type_info = _uri('user_money_type', $id);

        if (!$user_money_type_info) {
            return '请选择类型';
        }

        if (!$user_money_type_info['parent_id']) {
            $info['parent_id'] = 0;
            $info['parent_res_name'] = '顶级分类';
        } else {
            $parent_info = _uri('user_money_type', $user_money_type_info['parent_id']);
            $info['parent_res_name'] = $parent_info['res_name'];
        }

        $info['id'] = $user_money_type_info['id'];
        $info['res_name'] = $user_money_type_info['res_name'];

        Response::assign('user_money_type_info', $info);
        Response::display('admin/add_user_money_type.html');
    }

    /**
    * 更新
    */
    public function update()
    {
        $id = Request::getParam('id', 1);
        $user_money_type = Request::getParam('user_money_type', array());
        //检测余额类型是否存在
        $user_money_type_info = _model('user_money_type')->read(array('status'=>1, 'res_name'=>$user_money_type['res_name'], 'parent_id'=>$user_money_type['parent_id']));
        if (!$user_money_type_info) {
             _model('user_money_type')->update($id, array('res_name'=>$user_money_type['res_name']));
        }
        Response::assign('status', 1);
        Response::redirect('user_money/admin/user_money_type.html');
    }

    /**
    * 伪删除
    */
    public function delete()
    {
        //接收id
        $id  = Request::getParam('id', 0);

        $user_money_type_info = _uri('user_money_type', $id);
        if (!$user_money_type_info['parent_id']) {
            //判断此分类下有没有子分类
            $count = _model('user_money_type')->getTotal(array('parent_id'=>$user_money_type_info['id']));
            if ($count) {
                return '请先删除子类型';
            }
            
        }

        _model('user_money_type')->update($id, array('status' => 0));
        
        return 'ok';
    }

    /**
    * 还原
    */
    public function change_status()
    {
        //接收id
        $id = Request::Get('id', 1);

        _model('user_money_type')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('user_money/admin/user_money_type_list.html'));
    }

    /**
    * 检测余额类型是否存在
    */
    public function user_money_type_exists()
    {

        $user_money_type = Request::getParam('user_money_type', '');

        $filter = array();
        $filter['status'] = 1;
        $filter['res_name'] = $user_money_type;

        $user_money_type_info = _model('user_money_type')->read($filter);

        if ($user_money_type_info) {
            echo 'no';
        }
        echo 'ok';
    }

}
?>